package com.cskaoyan.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.converter.MemberConverter;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserLoginRequest;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.service.IUserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

/**
 * @Description 登录登出实现
 * @Author 徐成喜
 * @Data 2022/7/29 16:20
 */
@Service
public class UserLoginServiceImpl implements IUserLoginService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    MemberConverter memberConverter;

    /**
     * @return 返回一个token身份令牌
     * @description 登录功能，利用jwt
     */
    @Override
    public UserLoginResponse userLoginNoToken(UserLoginRequest request) {
        UserLoginResponse response = new UserLoginResponse();
        request.requestCheck();
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username",request.getUserName())
                .andEqualTo("password", DigestUtils.md5DigestAsHex(request.getPassword().getBytes()).trim());
        List<Member> members = memberMapper.selectByExample(example);
        if(members.isEmpty()||members.size()==0) {
            response.setCode(UserRetCode.USERORPASSWORD_ERRROR.getCode());
            response.setMsg(UserRetCode.USERORPASSWORD_ERRROR.getMessage());
            return response;
        }

        //是否激活
        Member member = members.get(0);
        if(member.getIsVerified().equals("N")){
            response.setCode(UserRetCode.USER_ISVERFIED_ERROR.getCode());
            response.setMsg(UserRetCode.USER_ISVERFIED_ERROR.getMessage());
            return response;
        }

        //已激活
            UserLoginResponse response1 = memberConverter.login2Member(member);
            HashMap<String, Object> map = new HashMap<>();
            map.put("uid",member.getId());
            map.put("username",member.getUsername());
            map.put("file",member.getFile());
            String token = JwtTokenUtils.builder().msg(JSON.toJSONString(map)).build().creatJwtToken();
            response1.setToken(token);
            response1.setCode(UserRetCode.SUCCESS.getCode());
            response1.setMsg(UserRetCode.SUCCESS.getMessage());
            // response1.setFile("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
            return response1;

//        }else {
//            Member member = members.get(0);
//            UserLoginResponse response1 = memberConverter.login2Member(member);
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("uid",member.getId());
//            map.put("username",member.getUsername());
//            String token = JwtTokenUtils.builder().msg(JSON.toJSONString(map)).build().creatJwtToken();
//            response1.setToken(token);
//            response1.setCode(UserRetCode.SUCCESS.getCode());
//            response1.setMsg(UserRetCode.SUCCESS.getMessage());
//            return response1;
//        }

    }
}
